﻿; (function (window, undefined, $) {
    $(document).ready(function () {
        var gridopt = {
            url: options.listUrl,
            colModel: [
                    { display: '子项目', name: 'SubProject', hide: true },
                    { display: '审核状态', name: 'AuditStatus', hide: true },
                    { display: '编号', name: 'ProjectNo', width: "10%", sortable: true, hide: true, align: 'left', iskey: true },
                    { display: '项目名称', name: 'ProjectName', width: "27%", sortable: false, align: 'left' },
                    { display: '贷款总额', name: 'TotalLoan', width: "10%", sortable: false, align: 'left', process: processTotalLoan },
                    { display: '已投金额', name: 'TenderAmt', width: "10%", sortable: false, align: 'left', process: processTotalLoan },
                    { display: '年收益率', name: 'AnnualisedRates', width: "5%", sortable: false, align: 'left', process: processAnnualisedRates },
                    { display: '附送收益', name: 'AddAnnualisedRates', width: "5%", sortable: false, align: 'left', process: processAnnualisedRates },
                    { display: '项目位置', name: 'ProjectLocation', width: "10%", sortable: false, align: 'left' },
                    { display: '子项目', name: 'ID', width: "5%", sortable: false, align: 'center', process: processSubProject },
                    { display: '操作', name: 'ID', width: "18%", sortable: false, align: 'center', process: processOp }
            ],
            sortname: "ID",
            sortorder: "ASC",
            title: false,
            rp: 15,
            usepager: true,
            showcheckbox: false
        };
        var xjgrid = new xjGrid("gridlist", gridopt);

        function processTotalLoan(value) {
            return formatNum(value);
        }

        function formatNum(v) {
            if (isNaN(v)) {
                return v;
            }
            v = (Math.round((v - 0) * 100)) / 100;
            v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v * 10)) ? v
                    + "0" : v);
            v = String(v);
            var ps = v.split('.');
            var whole = ps[0];
            var sub = ps[1] ? '.' + ps[1] : '.00';
            var r = /(\d+)(\d{3})/;
            while (r.test(whole)) {
                whole = whole.replace(r, '$1' + ',' + '$2');
            }
            v = whole + sub;

            return v;
        }

        function processAnnualisedRates(value) {
            var ops = [];
            ops.push(Math.round(value * 1000)/10.0, "%");
            return ops.join("");
        }

        function processSubProject(value, cell) {
            var ops = [];
            if (cell[0] == 1) {
                ops.push("&nbsp;<a title='子项目' href='javascript:;'  onclick=\"util.PetProject('", value, "')\"><i class='fa fa-edit' ></i>子项目</a>");
            }
            return ops.join("");
        }

        function processOp(value, cell) {
            var ops = [];
            ops.push("&nbsp;<a title='查看' href='javascript:;'  onclick=\"util.Detail('", value, "')\"><i class='fa fa-edit' ></i>查看</a>");
            if (cell[1] == 1) {
                ops.push("&nbsp;<a title='编辑' href='javascript:;'  onclick=\"util.Edit('", value, "')\"><i class='fa fa-edit' ></i>编辑</a>");
                //ops.push("&nbsp;&nbsp;<a title='删除' class='abtn' href='javascript:;'  onclick=\"util.Delete('", value, "','", cell[1], "')\"><i class='fa fa-trash-o' ></i>删除</a>");
            } else {
                ops.push("&nbsp;<i class='fa fa-edit' ></i>编辑");
                //ops.push("&nbsp;&nbsp;<i class='fa fa-trash-o' ></i>删除");
            }
            ops.push("&nbsp;<a title='查看' href='javascript:;'  onclick=\"util.Invesment('", value, "')\"><i class='fa fa-edit' ></i>投资情况</a>");
            return ops.join("");
        }

        $("#formQuery").submit(function () {
            xjgrid.Query(this);
            return false;
        });
        $("#btnSearch").click(function () {
            $("#formQuery").submit();
        });
        $("#btnAdd").click(function (e) {
            var url = options.editUrl + "?status=1";
            location.href = url;
        });
        //子项目的超链接
        util.PetProject = function (id) {
            var url = "PetProject?id=" + id;
            location.href = url;
        };

        util.Invesment = function (id) {
            var url = options.InvesmentUrl + "?id=" + id + "&status=1";
            location.href = url;
        };
        //查看细节
        util.Detail = function (id) {
            var url = options.detailUrl + "?id=" + id + "&status=1";
            location.href = url;
        };

        util.Edit = function (id) {
            var url = options.editUrl + "?id=" + id + "&status=1";
            location.href = url;
        };

        util.Audit = function (id) {
            var url = options.editUrl + "?id=" + id + "&status=2";
            location.href = url;
        };

        util.RepayPres = function (id) {
            var url = "/Project/RepayPres.html/" + id;
            location.href = url;
        };

        util.Delete = function (id, name) {
            if (!confirm("您确定要执行删除操作吗？")) {
                return false;
            }
            $.post(options.deleteUrl + "/" + id, { id: id },
                  function (res) {
                      if (res.Status > 0) {
                          alert("操作成功！");
                          xjgrid.Reload();
                      }
                      else {
                          alert("操作失败！");
                      }
                  },
                  "json"
            );
        };
    });

})(window, undefined, jQuery);